
function createDrugLot(){
    if(!dijit.byId('lotForm').validate()){
        return;
    }
    var lib = new DojoLib();
    
    var obj = new Generic();
    obj.set('drug_lot_name', dijit.byId('lotName').get('value'));
    obj.set('drug_lot_desc', dijit.byId('lotDesc').get('value'));
    obj.set('drug_lot_date', lib.getDateForJson('lotDate') + ' 00:00:00');
        
    var lotId = document.getElementById('lotId').value;
    var url = '';
    if(lotId == ''){
        url = 'services/manage_lot/create_drug_lot.php';
    }else{
        url = 'services/manage_lot/update_drug_lot.php';
        obj.set('drug_lot_id', document.getElementById('lotId').value);
    }
    
    lib.postJson(url, obj.toJson(), function(res){
        document.getElementById('lotId').value = res.drug_lot_id;
        dijit.byId('lotListPane').set('open', true);
        refereshLotListItemGrid();
    });
}

function refereshLotListGrid(){
    var lib = new DojoLib();
    lib.bindGridStore('services/manage_lot/retrieve_drug_lot_list.php', '', 'lotListGrid');
}

function refereshLotListItemGrid(){
    var obj = new Generic();
    obj.set('drug_lot_id', document.getElementById('lotId').value);
    
    var lib = new DojoLib();
    lib.bindGridStore('services/manage_lot/retrieve_drug_lot_item_list.php', obj.toJson(), 'drugLotListItemGrid');
}

function loadLotForm(id){
    var obj = new Generic();
    obj.set('drug_lot_id', id);
        
    var lib = new DojoLib();
    lib.postJson('services/manage_lot/get_drug_lot.php', obj.toJson(), function(res){
        //alert(res);
        document.getElementById('lotId').value = res.drug_lot_id;
        dijit.byId('lotName').set('value', res.drug_lot_name);
        dijit.byId('lotDesc').set('value', res.drug_lot_desc);
        dijit.byId('lotDate').set('displayedValue', res.drug_lot_date);
        
        dijit.byId('lotListPane').set('open', true);
        refereshLotListItemGrid();
    });
}

function openEditDrugLotDialog(id){
    dijit.byId('workPane').set('href', 'htmls/manage_lot/lot_form.php?drug_lot_id=' + id);
}

function checkDrugLotToDelete(id){
    alert('ตรวจสอบว่าล็อตนี้มีใช้งานไปหรือยัง');
}

function addLotItem(){
    openLotItemDialog('เพิ่มยาในล็อต', 'เพิ่ม', function(){
        var lib = new DojoLib();
        lib.bindComboStore('services/manage_drug/retrieve_drug_list.php', '', 'drugLotItemCombo');
        lib.bindComboStore('constant/drug_item_unit.json', '', 'drugLotItemUnitCombo');
    }, function(){
        if(!dijit.byId('drugLotItemForm').validate()){
            return;
        }
        
        var lib = new DojoLib();
        
        var obj = new Generic();
        obj.set('drug_lot_id', document.getElementById('lotId').value);
        obj.set('drug_id', dijit.byId('drugLotItemCombo').get('value'));
        obj.set('drug_lot_note', dijit.byId('lotNote').get('value'));
        obj.set('drug_expire_datetime', lib.getDateForJson('expireDate') + ' 00:00:00');
        obj.set('drug_lot_amount', dijit.byId('lotAmount').get('value') + '');
        
        lib.postJson('services/manage_lot/add_lot_item.php', obj.toJson(), function(res){
            if(res.status == 'fail'){
                alert(res.message);
            }else{
                refereshLotListItemGrid();
                dijit.byId('drugLotItemFormDialog').hide();
            }
        });
    })
}

/**
* 
ถ้าจำนวนติดลบแสดงว่านำออก จำเป็นต้องกรอก วันหมดอายุ
ให้ตรงกับ ล็อตที่มีด้วย
* */
function validateExpireDate(){
    var lotAmount = dijit.byId('lotAmount').get('value');
    if(parseInt(lotAmount) <= 0){
        dijit.byId('expireDate').set('required', true);
    }else{
        dijit.byId('expireDate').set('required', true);
    }
}

function openLotItemDialog(title, okBtnLabel, handleOnLoadDialog, handleOkBtn){
    var lotItemDialog = new dijit.Dialog({
        title : title,
        id : "drugLotItemFormDialog",
        style : "width:600px; height:240px;",
        href : "htmls/manage_lot/drug_lot_item_form.html",
        onHide : function() {
            this.destroyRecursive();
        },
        onLoad : function() {
            dijit.byId('drugLotItemOkBtn').set('label', okBtnLabel);
            dijit.byId('drugLotItemOkBtn').set('onClick', function(){
                handleOkBtn();
            });
            dijit.byId('drugLotItemCancelBtn').set('onClick', function(){
                lotItemDialog.hide();
            });
            dijit.byId('drugLotItemAddOtherDrugBtn').set('onClick', function(){
                addOtherDrugItem();
            });
            handleOnLoadDialog();
        }
    });
    lotItemDialog.show();
}

function addOtherDrugItem(){
    openDrugItemFormDialog('รายละเอียดตัวยา', 'เพิ่มยา', function(){}, function(){
        var obj = new Generic();
        obj.set('drug_name', dijit.byId('drugName').get('value'));
        obj.set('drug_detail', dijit.byId('drugDetail').get('value'));
        
        var lib = new DojoLib();
        lib.postJson('services/manage_drug/add_drug_item.php', obj.toJson(), function(res){
            lib.bindComboStore('services/manage_drug/retrieve_drug_list.php', '', 'drugLotItemCombo');
            dijit.byId('drugItemFormDialog').hide();
        });
    });
}

function deleteDrugLotItem(id){
    var obj = new Generic();
    obj.set('drug_lot_item_id', id + '');
        
    var lib = new DojoLib();
    lib.postJson('services/manage_lot/remove_lot_item.php', obj.toJson(), function(res){
        refereshLotListItemGrid();
    });
}